<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品咨询操作功能
 */
define('SHOP_BACKEND_CONSULT_LOOK',5030);		//商品咨询查看权限
define('SHOP_BACKEND_CONSULT_EDIT',5032);		//商品咨询编辑权限

require 'module/base/goodsHandler.class.php';

class pa_goodsConsult {
	
	public function execute () {
		
		if (isset($_REQUEST['operFlg'])) {
			
			//获取参数
			$operFlg = $_REQUEST['operFlg'];
			switch ($operFlg) {
				case 1: //商品咨询页面初始化
					$this->goodsConsultList();
					break;
				case 2: //商品咨询处理
					$this->dealGoodsConsult();
					break;
				case 3: //商品咨询导出
					$this->goodsConsultExport();
					break;
			}
		}
	}
	/**
	 * 商品咨询列表
	 * @author raohenglei
	 */
	private  function goodsConsultList(){
		global $G_PGLOB;
		
		$startDate = date('Y-m'.'-01'); //默认查询开始时间
		$endDate = date('Y-m-d');	//默认查询结束时间
		
		$G_PGLOB->smarty->assign('startDate',$startDate);
		$G_PGLOB->smarty->assign('endDate',$endDate);
		
		$G_PGLOB->smarty->display('content/goodsConsult.tpl');
	}
	
	/**
	 * 商品咨询处理
	 * @author raohenglei
	 *
	 */
	private function dealGoodsConsult () {
		global $G_PGLOB;
		try{
			$uuid = isset($_REQUEST['uuid'])?$_REQUEST['uuid']:NULL;
			
			if(empty($uuid))
				throw new Exception('参数不正确');
				
			$getConsultInfo = new entity('content_consult');
			$getConsultInfo->uuid = $uuid;
			if(!$G_PGLOB->DBCA->fetch($getConsultInfo))
				throw new Exception('商品咨询信息已经不存在');
				
			$getConsultInfo->consultInfo = htmlspecialchars($getConsultInfo->consultInfo);
			$getConsultInfo->answerInfo = str_replace(sprintf(' 感谢您对%s的支持！祝您购物愉快！',G_SHOP_NAME),'',$getConsultInfo->answerInfo);
			$G_PGLOB->smarty->assign('consultData',$getConsultInfo);
			
			$getConsultInfo->goodsLink = set_goods_link( $getConsultInfo->postID );
						
			if($G_PGLOB->has_permission(SHOP_BACKEND_CONSULT_EDIT))
				$G_PGLOB->smarty->assign('editFlg',1);
				
			$G_PGLOB->smarty->display('content/dealGoodsConsult.tpl');
		}catch (Exception $e){
			//error_log(__LINE__.'行 '. $e->getMessage());
			$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
		}
	}
	/**
	 * 商品咨询导出
	 *
	 */
	private function goodsConsultExport(){
		global $G_PGLOB;

		$startDate = isset($_REQUEST['startDate'])?$_REQUEST['startDate']:'';
		$endDate = isset($_REQUEST['endDate'])?$_REQUEST['endDate']:'';
		$consultStatus = isset($_REQUEST['consultStatus'])?$_REQUEST['consultStatus']:'';
		$displayFlg = isset($_REQUEST['displayFlg'])?$_REQUEST['displayFlg']:'';
		$goodsName =isset($_REQUEST['goodsName'])?trim($_REQUEST['goodsName']):'';
		
		$sqlStr = '1=1';
		
		if($startDate != ''){
			$sqlStr .= ' and date(createTime) >= \''.$startDate.'\'';
		}
		if($endDate != ''){
			$sqlStr .= ' and date(createTime) <= \''.$endDate.'\'';
		}
		
		if($consultStatus != '' || $consultStatus === 0){
			$sqlStr .= ' and consultStatus = \''.$consultStatus.'\'';
		}
		if($displayFlg != ''){
			$sqlStr .= ' and displayFlg = \''.$displayFlg.'\'';
		}
		if($goodsName != ''){
			$sqlStr .= ' and goodsName like \'%'.$goodsName.'%\'';
		}
		$sqlStr .= ' order by createTime desc'; 
		try{
			$sql = 'SELECT UUID,creator,createTime,consultInfo,goodsName,consultStatus,
					  answerUserID,answerDateTime,displayFlg,recommendFlg,postID,answerInfo 
					  FROM content_consult where '.$sqlStr;
			$datas = &$G_PGLOB->DBCA->stmt_query( $sql ); 
			
			header("Content-type: application/vnd.ms-csv; charset=utf-8");
			header("Content-Disposition: attachment; filename=".iconv('UTF-8', 'GBK','商品咨询导出信息').".csv");
			
			$fileHead = '商品名称,咨询内容,咨询时间,网站用户名,回复时间,回复人,处理状态,咨询回复'."\n";
			
	   		$content = '';
	   		for($i=0;$i<count($datas);$i++){
	   			$goodsName = str_replace(',','，',$datas[$i]->goodsName);
	   			$content .= $goodsName.',';
	   			$consultInfo = str_replace(',','，',$datas[$i]->consultInfo);
	   			$content .= $consultInfo.',';
	   			$content .= $datas[$i]->createTime.',';
	   			$creator = str_replace(',','，',$datas[$i]->creator);
	   			$content .= $creator.',';
	   			$content .= $datas[$i]->answerDateTime.',';
	   			$answerUserID = str_replace(',','，',$datas[$i]->answerUserID);
	   			$content .= $answerUserID.',';
	   			$content .= $datas[$i]->consultStatus == 1 ? "已处理,":"未处理,";
	   			$answerInfo = str_replace(',','，',$datas[$i]->answerInfo);
	   			$content .= $answerInfo."\n";
	   		}
	   		
	   		$insert = $fileHead.$content;
	   		$result = iconv('utf-8','gbk',$insert);
	   		echo $result;
	   		//echo $insert;

		}catch(Exception $e){
			$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
		}
	}
	
}